package com.cyandpos.pad.sale.view;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.cyandpos.pad.R;
import com.cyandpos.pad.utils.CommonUtils;

/**
 * Created by xin on 2021/5/28
 * desc:  数字键盘
 */
public class SelfNumKeyView extends LinearLayout {

    Button btnSevenKeyboard;
    Button btnEightKeyboard;
    Button btnNineKeyboard;
    Button btnFourKeyboard;
    Button btnFiveKeyboard;
    Button btnSixKeyboard;
    Button btnOneKeyboard;
    Button btnTwoKeyboard;
    Button btnThreeKeyboard;
    Button btnPointKeyboard;
    Button btnZeroKeyboard;
    Button btnBackKeyboard;
    Button btnClearKeyboard;
    Button btnSureKeyboard;

    EditText etMainSoftKeyboard;//录入框
    Boolean isCanInputPoint = false;//是否可以输入小数点
    Boolean isClearEt = false;//输入前是否清空edittext
    private SelfNumCallBack mCallBack;// 回调
    private String etHint ="";

    public SelfNumKeyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.self_soft_keyboard, this);
        btnZeroKeyboard = findViewById(R.id.btn_zero_keyboard);
        btnOneKeyboard = findViewById(R.id.btn_one_keyboard);
        btnTwoKeyboard = findViewById(R.id.btn_two_keyboard);
        btnThreeKeyboard = findViewById(R.id.btn_three_keyboard);
        btnFourKeyboard = findViewById(R.id.btn_four_keyboard);
        btnFiveKeyboard = findViewById(R.id.btn_five_keyboard);
        btnSixKeyboard = findViewById(R.id.btn_six_keyboard);
        btnSevenKeyboard = findViewById(R.id.btn_seven_keyboard);
        btnEightKeyboard = findViewById(R.id.btn_eight_keyboard);
        btnNineKeyboard = findViewById(R.id.btn_nine_keyboard);
        btnPointKeyboard = findViewById(R.id.btn_point_keyboard);
        btnBackKeyboard = findViewById(R.id.btn_back_keyboard);
        btnClearKeyboard = findViewById(R.id.btn_clear_keyboard);
        btnSureKeyboard = findViewById(R.id.btn_sure_keyboard);

        btnZeroKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnOneKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnTwoKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnThreeKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnFourKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnFiveKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnSixKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnSevenKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnEightKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnNineKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnPointKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnBackKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnClearKeyboard.setOnClickListener(new KeyBoardOnClickListener());
        btnSureKeyboard.setOnClickListener(new KeyBoardOnClickListener());

    }

    /**
     *  设置接收者
     * @param et 接收者
     */
    public void setEnterReceptor(EditText et) {
        etMainSoftKeyboard = et;
    }
    /**
     *  设置接收框是否允许输入小数点
     * @param isHasPoint 是否允许输入小数点
     */
    public void setIsCanInputPoint(Boolean isHasPoint) {
        isCanInputPoint = isHasPoint;
    }

    /**
     *  设置接收框输入前是否清空
     * @param
     */
    public void setIsClearEt(Boolean isClearEt) {
        this.isClearEt = isClearEt;
    }
    /**
     * 设置接收者的提示信息
     * @param hint
     */
    public void setNumKeyBoardHint(String hint){
        etHint = hint;
        etMainSoftKeyboard.setHint(hint);
        etMainSoftKeyboard.setText("");
    }
    public String getNumKeyBoardHint(){
       return etHint;
    }

    public void clearReceptorText(){
        etMainSoftKeyboard.setText("");
    }

    class KeyBoardOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_zero_keyboard:
                    calInput("0");
                    break;
                case R.id.btn_one_keyboard:
                    calInput("1");
                    break;
                case R.id.btn_two_keyboard:
                    calInput("2");
                    break;
                case R.id.btn_three_keyboard:
                    calInput("3");
                    break;
                case R.id.btn_four_keyboard:
                    calInput("4");
                    break;
                case R.id.btn_five_keyboard:
                    calInput("5");
                    break;
                case R.id.btn_six_keyboard:
                    calInput("6");
                    break;
                case R.id.btn_seven_keyboard:
                    calInput("7");
                    break;
                case R.id.btn_eight_keyboard:
                    calInput("8");
                    break;
                case R.id.btn_nine_keyboard:
                    calInput("9");
                    break;
                case R.id.btn_point_keyboard:
                    calInput(".");
                    break;
                case R.id.btn_back_keyboard:
                    calBack();
                    break;
                case R.id.btn_clear_keyboard:
                    calClear();
                    break;
                case R.id.btn_sure_keyboard:
                    calSure();
                    break;
                default:
                    break;
            }
        }
    }

    private void calInput(String num) {
        if(etMainSoftKeyboard==null){
//            ToastUtils.show("请点击需要输入内容接收框！");
        }else{
            if(num.equals(".")){
                if(isCanInputPoint)
                    CommonUtils.insertEditTextContext(etMainSoftKeyboard,num);
            }else{
                if(isClearEt){
                    CommonUtils.clearInsertEditTextContext(etMainSoftKeyboard,num);
                    isClearEt=false;
                }else {
                    CommonUtils.insertEditTextContext(etMainSoftKeyboard, num);
                }
            }
        }
    }

    private void calBack() {
        if(etMainSoftKeyboard==null){
//            ToastUtils.show("请点击需要输入内容接收框！");
        }else{
            CommonUtils.delectEditTextContext(etMainSoftKeyboard);
        }

    }

    private void calClear() {
        if(etMainSoftKeyboard==null){
//            ToastUtils.show("请点击需要输入内容接收框！");
        }else{
            etMainSoftKeyboard.setText("");
        }
    }

    private void calSure() {
        if(etMainSoftKeyboard==null){
//            ToastUtils.show("请点击需要输入内容接收框！");
        }else{
            if(mCallBack!=null){
                String inputStr = etMainSoftKeyboard.getText().toString();
                mCallBack.keyBoardSure(inputStr,etHint);
            }
        }
    }

    public interface SelfNumCallBack {
        void keyBoardSure(String num,String hint);//确定后
    }

    public void setOnCallBack(SelfNumCallBack callBack) {
        mCallBack = callBack;
    }

}
